package com.siyougame.business.controller.app;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.io.IoUtil;
import com.siyougame.business.controller.admin.inspect.vo.*;
import com.siyougame.business.controller.app.vo.AppInspectCreateReqVO;
import com.siyougame.business.controller.app.vo.AppInspectPageReqVO;
import com.siyougame.business.controller.app.vo.AppInspectRespVO;
import com.siyougame.business.convert.inspect.InspectConvert;
import com.siyougame.business.dal.dataobject.inspect.InspectDO;
import com.siyougame.business.dal.dataobject.user.WxUserDO;
import com.siyougame.business.service.inspect.InspectService;
import com.siyougame.common.annotation.OperateLog;
import com.siyougame.common.core.domain.AjaxResult;
import com.siyougame.common.pojo.CommonResult;
import com.siyougame.common.pojo.PageResult;
import com.siyougame.common.utils.ExcelUtils;
import com.siyougame.file.service.FileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static com.siyougame.common.enums.OperateTypeEnum.EXPORT;
import static com.siyougame.common.pojo.CommonResult.success;

@Api(tags = "检查记录")
@RestController
@RequestMapping("/openapi/app/inspect")
@Validated
@SaCheckLogin
public class AppInspectController {

    @Resource
    private InspectService inspectService;
    @Resource
    private FileService fileService;

    @PostMapping("/create")
    @ApiOperation("创建检查记录")
    public CommonResult<Long> createInspect(@Valid @RequestBody AppInspectCreateReqVO createReqVO) {
        return success(inspectService.createInspect(createReqVO));
    }



    @GetMapping("/get")
    @ApiOperation("获得检查记录详情")
    public CommonResult<AppInspectRespVO> getInspect(@RequestParam("id") Long id) {
        return success(inspectService.getAppInspect(id));
    }


    @GetMapping("/page")
    @ApiOperation("获得检查记录分页")
    public CommonResult<PageResult<AppInspectRespVO>> getInspectPage(@Valid AppInspectPageReqVO pageVO) {
        PageResult<InspectDO> pageResult = inspectService.getInspectPage(pageVO);
        return success(InspectConvert.INSTANCE.convertAppPage(pageResult));
    }
    /**
     * 导出检查记录
     * @return
     */
    @GetMapping("/export")
    @ApiOperation("导出检查记录")
    public void exportInspect(@RequestParam("id")Long id, HttpServletResponse response) {
        inspectService.getAppInspectForExport(id, response);
    }
    @PostMapping("/upload")
    @ApiOperation("上传文件")
    @OperateLog(logArgs = false) // 上传文件，没有记录操作日志的必要
    public AjaxResult uploadFile(@RequestParam("file") MultipartFile file) throws Exception {
        String fileUrl = fileService.createFile1(file.getOriginalFilename(), "vessels-check", IoUtil.readBytes(file.getInputStream()));
        return AjaxResult.success(fileUrl);
    }
}
